<?php

class cache {

    var $db;
    var $cachefile;


    function cache(& $db) {
        $this->db=$db;
    }

    function getfile($cachename) {
        $this->cachefile=MY_ROOT.'/data/cache/'.$cachename.'.php';
    }

    function isvalid($cachename,$cachetime) {
        if(0==$cachetime) {
            return true;
        }
        $this->getfile($cachename);
        if(!is_readable($this->cachefile)||$cachetime<0) {
            return false;
        }
        clearstatcache();
        return (time()-filemtime($this->cachefile))<$cachetime;
    }

    function read($cachename,$cachetime=0) {
        $this->getfile($cachename);
        if($this->isvalid($cachename,$cachetime)) {
            return @include $this->cachefile;
        }
        return false;
    }

    function write($cachename, $arraydata) {
        $this->getfile($cachename);
        if(!is_array($arraydata)) return false;
        $strdata = "<?php\nreturn ".var_export($arraydata, true).";\n?>";
        $bytes = writetofile($this->cachefile, $strdata);
        return $bytes;
    }

    function remove($cachename) {
        $this->getfile($cachename);
        if(file_exists($this->cachefile)) {
            unlink($this->cachefile);
        }
    }

    function load($cachename,$id='id',$orderby='') {
        $arraydata=$this->read($cachename);
        if(!$arraydata) {
            $sql='SELECT * FROM '.DB_TABLEPRE.$cachename;
            $orderby && $sql.=" ORDER BY $orderby ASC";
            $query=$this->db->query($sql);
            while($item=$this->db->fetch_array($query)) {
                if( isset($item['k']) ) {
                    $arraydata[$item['k']]=$item['v'];
                }else {
                    $arraydata[$item[$id]]=$item;
                }
            }
            $this->write($cachename,$arraydata);
        }
        return $arraydata;
    }
    function loadarea($id='7') {
    	$cachename = 'area_'.$id;
    	$arraydata=$this->read($cachename);
    	if(!$arraydata) {
            $sql="SELECT id,name FROM ".DB_TABLEPRE."area where pid='$id' order by id asc";
            $query=$this->db->query($sql);
            while($item=$this->db->fetch_array($query)) {
                $arraydata[$item['id']]['name']=$item['name'];
                $qt=$this->db->query("SELECT id,name FROM ".DB_TABLEPRE."area where pid='$item[id]' order by id asc");
                while($it=$this->db->fetch_array($qt)) {
                	$arraydata[$item['id']]['foot'][$it['id']]=$it['name'];
                }
            }
            $this->write($cachename,$arraydata);
        }
        return $arraydata;
    }
    function loadcatemod($cachename) {
    	if($cachename == 'catemod'){
    		$mid =1;
    	}else{
    		$mid=0;
    	}
    	$arraydata=$this->read($cachename);
    	if(!$arraydata) {
            $sql="SELECT id,name,`type` FROM ".DB_TABLEPRE."catesys where mid='$mid' order by id asc";
            $query=$this->db->query($sql);
            while($item=$this->db->fetch_array($query)) {
                $arraydata[$item['id']]['name']=$item['name'];
                $arraydata[$item['id']]['type']=$item['type'];
                $qt=$this->db->query("SELECT id,name FROM ".DB_TABLEPRE."cate where tid='$item[id]' order by views desc");
                while($it=$this->db->fetch_array($qt)) {
                	$arraydata[$item['id']]['foot'][$it['id']]=$it['name'];
                }
            }
            $this->write($cachename,$arraydata);
        }
        return $arraydata;
    }
    function loadhotsite() {
        $cachename = 'hotsite';
    	$arraydata=$this->read($cachename);
    	if(!$arraydata) {
            $sql="SELECT id,name FROM ".DB_TABLEPRE."site where available='1' order by displayorder asc limit 0,20";
            $query=$this->db->query($sql);
            while($item=$this->db->fetch_array($query)) {
                $arraydata[$item['id']]=$item['name'];
            }
            $this->write($cachename,$arraydata);
        }
        return $arraydata;
    }
    /*
    function loadsitecate() {
    	$sql="SELECT id FROM ".DB_TABLEPRE."site where available='1'";
        $query=$this->db->query($sql);
        while($item=$this->db->fetch_array($query)) {
            $this->loadsitecateone($item['id']);
        }
    }
    */
    function loadsitecate($id) {
        $cachename = 'sitecate_'.$id;
       
    	$arraydata=$this->read($cachename);
    	 
    	if(!$arraydata) {
            $str = '';
			$arraydata = $sys = array ();
			$sql="SELECT id,name FROM my_cateconfig";
	        $query=$this->db->query($sql);
	        while($item=$this->db->fetch_array($query)) {
	                $sys[$item['id']]=$item['name'];
	        }
			//$sys = array(1=>'周边游',2=>'国内游',3=>'出境游');
			foreach($sys as $i => $n ) {
				$mdb = $this->db->query ( "SELECT m.cid,c.name FROM `" . DB_TABLEPRE . "mapsitecate` as m left join `" . DB_TABLEPRE . "cate` as c on m.cid=c.id WHERE m.sid='$id' and m.tid='$i'" );
				while ( $m = $this->db->fetch_array ( $mdb ) ) {
					$arraydata[$i]['name'] = $n;
					$arraydata[$i][$m['cid']]['top'] = $m['name'];
										
					$db = $this->db->query ( "SELECT m.aid,s.name,s.ename FROM `" . DB_TABLEPRE . "mapsitearea` as m left join `" . DB_TABLEPRE . "area` as s on m.aid=s.id WHERE m.sid='$id' and m.cid='$m[cid]' and m.tid='$i'" );
					while ( $t = $this->db->fetch_array ( $db ) ) {
						if(!empty($t['ename'])){
							$t['name'] = $t['ename'];
						}
						$arraydata[$i][$m['cid']]['foot'][] = $t;
					}
					
					
				}
			}
            $this->write($cachename,$arraydata);
        }
        //var_dump($arraydata);exit();
        //var_dump($arraydata);exit();
        return $arraydata;
    }

}

?>